/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Controladores;

import Controladores.exceptions.IllegalOrphanException;
import Controladores.exceptions.NonexistentEntityException;
import java.io.Serializable;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import Entidades.Catedratico;
import Entidades.Gradoacademico;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;

/**
 *
 * @author Miguel
 */
public class GradoacademicoJpaController implements Serializable {

    public GradoacademicoJpaController(EntityManagerFactory emf) {
        this.emf = emf;
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(Gradoacademico gradoacademico) {
        if (gradoacademico.getCatedraticoCollection() == null) {
            gradoacademico.setCatedraticoCollection(new ArrayList<Catedratico>());
        }
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Collection<Catedratico> attachedCatedraticoCollection = new ArrayList<Catedratico>();
            for (Catedratico catedraticoCollectionCatedraticoToAttach : gradoacademico.getCatedraticoCollection()) {
                catedraticoCollectionCatedraticoToAttach = em.getReference(catedraticoCollectionCatedraticoToAttach.getClass(), catedraticoCollectionCatedraticoToAttach.getNumeroCatedratico());
                attachedCatedraticoCollection.add(catedraticoCollectionCatedraticoToAttach);
            }
            gradoacademico.setCatedraticoCollection(attachedCatedraticoCollection);
            em.persist(gradoacademico);
            for (Catedratico catedraticoCollectionCatedratico : gradoacademico.getCatedraticoCollection()) {
                Gradoacademico oldGradoAcademicoidGradoAcademicoOfCatedraticoCollectionCatedratico = catedraticoCollectionCatedratico.getGradoAcademicoidGradoAcademico();
                catedraticoCollectionCatedratico.setGradoAcademicoidGradoAcademico(gradoacademico);
                catedraticoCollectionCatedratico = em.merge(catedraticoCollectionCatedratico);
                if (oldGradoAcademicoidGradoAcademicoOfCatedraticoCollectionCatedratico != null) {
                    oldGradoAcademicoidGradoAcademicoOfCatedraticoCollectionCatedratico.getCatedraticoCollection().remove(catedraticoCollectionCatedratico);
                    oldGradoAcademicoidGradoAcademicoOfCatedraticoCollectionCatedratico = em.merge(oldGradoAcademicoidGradoAcademicoOfCatedraticoCollectionCatedratico);
                }
            }
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(Gradoacademico gradoacademico) throws IllegalOrphanException, NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Gradoacademico persistentGradoacademico = em.find(Gradoacademico.class, gradoacademico.getIdGradoAcademico());
            Collection<Catedratico> catedraticoCollectionOld = persistentGradoacademico.getCatedraticoCollection();
            Collection<Catedratico> catedraticoCollectionNew = gradoacademico.getCatedraticoCollection();
            List<String> illegalOrphanMessages = null;
            for (Catedratico catedraticoCollectionOldCatedratico : catedraticoCollectionOld) {
                if (!catedraticoCollectionNew.contains(catedraticoCollectionOldCatedratico)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain Catedratico " + catedraticoCollectionOldCatedratico + " since its gradoAcademicoidGradoAcademico field is not nullable.");
                }
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            Collection<Catedratico> attachedCatedraticoCollectionNew = new ArrayList<Catedratico>();
            for (Catedratico catedraticoCollectionNewCatedraticoToAttach : catedraticoCollectionNew) {
                catedraticoCollectionNewCatedraticoToAttach = em.getReference(catedraticoCollectionNewCatedraticoToAttach.getClass(), catedraticoCollectionNewCatedraticoToAttach.getNumeroCatedratico());
                attachedCatedraticoCollectionNew.add(catedraticoCollectionNewCatedraticoToAttach);
            }
            catedraticoCollectionNew = attachedCatedraticoCollectionNew;
            gradoacademico.setCatedraticoCollection(catedraticoCollectionNew);
            gradoacademico = em.merge(gradoacademico);
            for (Catedratico catedraticoCollectionNewCatedratico : catedraticoCollectionNew) {
                if (!catedraticoCollectionOld.contains(catedraticoCollectionNewCatedratico)) {
                    Gradoacademico oldGradoAcademicoidGradoAcademicoOfCatedraticoCollectionNewCatedratico = catedraticoCollectionNewCatedratico.getGradoAcademicoidGradoAcademico();
                    catedraticoCollectionNewCatedratico.setGradoAcademicoidGradoAcademico(gradoacademico);
                    catedraticoCollectionNewCatedratico = em.merge(catedraticoCollectionNewCatedratico);
                    if (oldGradoAcademicoidGradoAcademicoOfCatedraticoCollectionNewCatedratico != null && !oldGradoAcademicoidGradoAcademicoOfCatedraticoCollectionNewCatedratico.equals(gradoacademico)) {
                        oldGradoAcademicoidGradoAcademicoOfCatedraticoCollectionNewCatedratico.getCatedraticoCollection().remove(catedraticoCollectionNewCatedratico);
                        oldGradoAcademicoidGradoAcademicoOfCatedraticoCollectionNewCatedratico = em.merge(oldGradoAcademicoidGradoAcademicoOfCatedraticoCollectionNewCatedratico);
                    }
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = gradoacademico.getIdGradoAcademico();
                if (findGradoacademico(id) == null) {
                    throw new NonexistentEntityException("The gradoacademico with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws IllegalOrphanException, NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Gradoacademico gradoacademico;
            try {
                gradoacademico = em.getReference(Gradoacademico.class, id);
                gradoacademico.getIdGradoAcademico();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The gradoacademico with id " + id + " no longer exists.", enfe);
            }
            List<String> illegalOrphanMessages = null;
            Collection<Catedratico> catedraticoCollectionOrphanCheck = gradoacademico.getCatedraticoCollection();
            for (Catedratico catedraticoCollectionOrphanCheckCatedratico : catedraticoCollectionOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This Gradoacademico (" + gradoacademico + ") cannot be destroyed since the Catedratico " + catedraticoCollectionOrphanCheckCatedratico + " in its catedraticoCollection field has a non-nullable gradoAcademicoidGradoAcademico field.");
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            em.remove(gradoacademico);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<Gradoacademico> findGradoacademicoEntities() {
        return findGradoacademicoEntities(true, -1, -1);
    }

    public List<Gradoacademico> findGradoacademicoEntities(int maxResults, int firstResult) {
        return findGradoacademicoEntities(false, maxResults, firstResult);
    }

    private List<Gradoacademico> findGradoacademicoEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(Gradoacademico.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public Gradoacademico findGradoacademico(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Gradoacademico.class, id);
        } finally {
            em.close();
        }
    }

    public int getGradoacademicoCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<Gradoacademico> rt = cq.from(Gradoacademico.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
